package logic

import (
	"context"
	"fmt"
	"zhihu/models/mysql"

	"zhihu/zhihurpc/internal/svc"
	"zhihu/zhihurpc/zhihurpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AnswersAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAnswersAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AnswersAddLogic {
	return &AnswersAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AnswersAddLogic) AnswersAdd(in *zhihurpc.AnswersAddRequest) (*zhihurpc.AnswersAddResponse, error) {
	// todo: add your logic here and delete this line
	answers := mysql.Answers{
		Content:   in.Content,
		ProblemId: int(in.ProblemId),
		UserId:    int(in.UserId),
	}
	err := mysql.DB.Debug().Create(&answers).Error
	if err != nil {
		return nil, fmt.Errorf("评论发表失败")
	}

	return &zhihurpc.AnswersAddResponse{
		AnswerId: int64(answers.ID),
	}, nil
}
